package com.dianxin.smartpark.service.park.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkArea;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkAreaExample;
import com.dianxin.smartpark.dao.mapper.park.SmartParkAreaMapper;
import com.dianxin.smartpark.dao.qo.park.SmartParkAreaQO;
import com.dianxin.smartpark.service.park.SmartParkAreaService;
import com.dianxin.smartpark.service.vo.park.SmartParkAreaVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 区域管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-08-19
 */
@Service
public class SmartParkAreaServiceImpl implements SmartParkAreaService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkAreaServiceImpl.class);
    @Resource
    private SmartParkAreaMapper smartParkAreaMapper;

    /**
     * 查询区域管理
     * 
     * @param qo 区域管理
     * @return 区域管理
     */
    @Override
    public CommonResponseBody<SmartParkAreaVO> selectSmartParkArea(SmartParkAreaQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkAreaExample smartParkAreaExample = new SmartParkAreaExample();
        SmartParkAreaExample.Criteria criteria = smartParkAreaExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkArea> passRecords = smartParkAreaMapper.selectByExample(smartParkAreaExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkAreaVO.class);
    }

    /**
     * 查询区域管理列表
     * 
     * @param qo 区域管理
     * @return 区域管理
     */
    @Override
    public CommonResponseBody<List<SmartParkAreaVO>> selectSmartParkAreaList(SmartParkAreaQO qo)
    {
        SmartParkAreaExample smartParkAreaExample = new SmartParkAreaExample();
        SmartParkAreaExample.Criteria criteria = smartParkAreaExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getAreaNo() != null) {
            criteria.andAreaNameEqualTo(qo.getAreaNo());
        }
        if (qo.getAreaType() != null) {
            criteria.andAreaTypeEqualTo(qo.getAreaType());
        }
        if (qo.getParentId() != null) {
            criteria.andParentIdEqualTo(qo.getParentId());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkAreaExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkAreaExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkArea> passRecords = smartParkAreaMapper.selectByExample(smartParkAreaExample);
        long count = smartParkAreaMapper.countByExample(smartParkAreaExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkAreaVO.class);
    }

    @Override
    public CommonResponseBody<List<Tree<String>>> selectSmartParkAreaTree(SmartParkAreaQO qo) {
        SmartParkAreaExample smartParkAreaExample = new SmartParkAreaExample();
        SmartParkAreaExample.Criteria criteria = smartParkAreaExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getAreaType() != null) {
            criteria.andAreaTypeEqualTo(qo.getAreaType());
        }
        if (qo.getAreaNo() != null) {
            criteria.andAreaNameEqualTo(qo.getAreaNo());
        }
        if (qo.getAreaName() != null) {
            criteria.andAreaNameLike("%"+qo.getAreaName()+"%");
        }
        criteria.andIsDeletedEqualTo(0);
        smartParkAreaExample.setOrderByClause("sort");
        List<SmartParkArea> passRecords = smartParkAreaMapper.selectByExample(smartParkAreaExample);
        if(StringUtils.isEmpty(passRecords)){
            return ResponseBodyWrapperUtil.fail("暂无数据","400");
        }
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setNameKey("areaName");

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(passRecords, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setWeight(treeNode.getSort());
                    tree.setName(treeNode.getAreaName());
                    tree.putExtra("areaNo",treeNode.getAreaNo());
                    tree.putExtra("areaType",treeNode.getAreaType());
                    tree.putExtra("label",treeNode.getAreaName());
                });
        return ResponseBodyWrapperUtil.successWrapper(treeNodes);
    }

    /**
     * 新增区域管理
     * 
     * @param vo 区域管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkArea(@Valid SmartParkAreaVO vo)
    {
        // 验证 todo
        SmartParkArea smartParkArea = new SmartParkArea();
        try {
            BeanUtil.insertConversion(vo, smartParkArea);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkArea.setId(IdUtil.createSnowflake(RandomUtil.randomLong(1, 30), 0).nextId());
        smartParkArea.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkAreaMapper.insertSelective(smartParkArea));
    }

    /**
     * 修改区域管理
     * 
     * @param vo 区域管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkArea(@Valid SmartParkAreaVO vo)
    {
        // 验证 todo
        SmartParkArea smartParkArea = new SmartParkArea();
        try {
            BeanUtil.insertConversion(vo, smartParkArea);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkArea.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkAreaMapper.updateByPrimaryKeySelective(smartParkArea));
    }

    /**
     * 删除区域管理信息
     * 
     * @param vo 区域管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkArea(@Valid SmartParkAreaVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkArea smartParkArea = new SmartParkArea();
        try {
            BeanUtil.insertConversion(vo, smartParkArea);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkArea.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkAreaMapper.updateByPrimaryKeySelective(smartParkArea));
    }
}
